clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
*compute aggregate LTG
use if LTG<. using $Work/ibes_ret, clear
gstats winsor LTG, cuts(1 99) by(datem) replace

bys datem: egen Tot=total(mcap)
gen vw=LTG*(mcap/Tot)

collapse (sum) LTG=vw, by(datem)
replace LTG=. if LTG==0

tsset datem

gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen dMoLLTG=LTG -LMLTG
gen dQrLLTG=LTG -LQLTG

forvalues x=1(1)5 { 
local t=12*`x'
gen  L`x'LTG=L`t'.LTG
gen  d`x'LLTG=LTG-L`x'LTG
}

foreach var in *LTG {
	rename `var' `var'agg
}

keep datem *agg
tempfile LTGagg
save "`LTGagg'", replace
* ************************************************
* generated by EBR_Monthly.do
use permco permno datem LTG medest* *FE* cp* EPS*Yr EPS book* bemeLTM mcap op inv BOOK INV PROF MOME SI* qruf1 cfacpr prc LTG *STG* using $Work/EBR_new_monthly, clear
keep if qruf1<.
drop SIZE
rename SI10 SIZE

tsset permno datem
gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen L1LTG=L12.LTG
gen L3LTG=L36.LTG
gen L5LTG=L60.LTG

gen LMSTG=L1.STG2
gen LQSTG=L3.STG2
gen L1STG=L12.STG2
gen L3STG=L36.STG2
gen L5STG=L60.STG2

gen dMoLLTG=LTG -L1.LTG
gen dQrLLTG=LTG -L3.LTG

gen dMoLSTG=STG2-L1.STG2
gen dQrLSTG=STG2-L3.STG2

tsset permno datem
forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
	gen d`x'LSTG=STG2-L`j'.STG2

}
gen LFE1=L12.FE1
gen LFE3=L36.FE3	
gen LFE5=L60.FE5
* ************************************************
gen lnLFE1=ln(EPS)/1-ln(L12.medest12)/1		if L12.medest12>0 
gen lnLFE3=ln(EPS)/3-ln(L36.medest36)/3		if L36.medest12>0 
gen lnLFE5=ln(EPS)/5-ln(L60.EPS)/5-L60.LTG	if L60.LTG>-1 

gstats winsor *FE* lnLF* cp* *LTG *STG* bemeLTM, cuts(1 99) by(datem) replace

tempfile march
save "`march'", replace
* ***************
eststo clear
foreach p in BOOK SIZE INV PROF MOME {
use if `p'<. using "`march'", clear
gen portf=1+int((`p'-1)/2)
gen d1LSTG2=STG2-L1STG2

collapse (mean) *LTG STG2 *STG LFE1 LFE3 LFE5 lnLFE* bemeLTM, by(datem portf)
gen lnbeme =ln(bemeLTM)
drop bemeLTM 
/* ///////////////////////////////////////////////// */
* GENERATE HML SPREADS for variables other than returns
preserve
keep if portf==1 | portf==5  
foreach var of varlist *LTG STG2 *STG LFE1 LFE3 LFE5 lnLFE* lnbeme*  {
	replace `var'=-`var' if portf==5 & index("`p'","INV")+index("`p'","SIZE")==1
	replace `var'=-`var' if portf==1 & index("`p'","INV")+index("`p'","SIZE")==0
}	
foreach v of varlist  LTG L*LTG d*LTG STG2 L*STG d*STG LFE1 LFE3 LFE5 lnLFE* lnbeme* {
bysort datem: egen count`v' = count(`v') 
}
collapse (sum)         LTG L*LTG d*LTG STG2 L*STG d*STG  lnLFE* lnbeme*  (mean) count*, by(datem)
foreach v of varlist   LTG L*LTG d*LTG STG2 L*STG d*STG  lnLFE* lnbeme*   {
replace `v'=. if count`v'!=2
}

generate portf=6

tempfile PORTF
save "`PORTF'", replace

restore
append using "`PORTF'"
merge 1:1 datem portf using $Work/portfolio_`p'_qnic, keepusing(portf datem cum* L12ret lnDP L*ret*)
drop _merge
rename lnDP lndp
merge m:1 datem using "`LTGagg'"
drop if _merge<3
drop _merge  

tsset portf datem
keep if L1LTG+d1LLTG+STG2+d1LLTGagg+L1LTGagg+lnLFE5<.
local Z "d*LLTG d*LSTG d*LLTG L*LTG L*STG lnLFE* *agg"   

* STANDARIZE VARIABLES
foreach var of varlist Lret* lndp lnbeme `Z' {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
* ------------------------------------------------------------------------------------------------------------------- *
* Run regressions for returns 
* ------------------------------------------------------------------------------------------------------------------- *
generate DLT=dMoLLTG
generate DST=dMoLSTG
generate LLT=LMLTG
generate LST=LMSTG
generate LFE=1
generate DLTa=dMoLLTGagg
generate LLTa=LMLTGagg
generate LRET=LretMo1
quietly: ivreg2 cumNRetMo1 lnbeme 							if portf==6, bw(1 ) robust small
scalar r2_1st= e(r2)
eststo beme_`p'_1: quietly: ivreg2 cumNRetMo1	lnbeme DLT LLT DST LST LFE DLTa LLTa	if portf==6, bw(1 ) robust small
estadd scalar r2_1st
scalar FF= r(F)
estadd scalar FF

quietly: ivreg2 cumNRetMo1 LRET 							if portf==6, bw(1 ) robust small
scalar r2_1st= e(r2)
eststo ret_`p'_1: quietly: ivreg2 cumNRetMo1 	LRET DLT LLT DST LST LFE DLTa LLTa	if portf==6, bw(1 ) robust small
estadd scalar r2_1st
scalar FF= r(F)
estadd scalar FF
* ------------------------------------------------------------------------------------------------------------------- *
replace DLT=d1LLTG
replace DST=d1LSTG
replace LLT=L1LTG
replace LST=L1STG
replace LFE=lnLFE1
replace DLTa=d1LLTGagg
replace LLTa=L1LTGagg
replace LRET=LretYr1	

quietly: ivreg2 cumNRetYr1 lnbeme 							if portf==6, bw(13) robust small
scalar r2_1st= e(r2)
eststo beme_`p'_2: quietly: ivreg2 cumNRetYr1	lnbeme DLT LLT DST LST LFE DLTa LLTa	if portf==6, bw(13) robust small
estadd scalar r2_1st
scalar FF= r(F)
estadd scalar FF

quietly: ivreg2	cumNRetYr1 LRET								if portf==6, bw(13) robust small
scalar r2_1st= e(r2)
eststo ret_`p'_2: quietly: ivreg2 cumNRetYr1 	LRET DLT LLT DST LST LFE DLTa LLTa	if portf==6, bw(13) robust small
estadd scalar r2_1st
scalar FF= r(F)
estadd scalar FF
}
esttab beme* using $Tables/AppendixC3_June.csv	, nonotes  compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a r2_1st) replace
esttab ret*  using $Tables/AppendixC3_June.csv	, nonotes  compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a r2_1st) append